<template>
  <div class="system-log-container">
    <div class="spaceForm">
      <el-form />
      <el-form>
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
      </el-form>
    </div>

    <el-table v-loading="listLoading" :data="list">
      <el-table-column align="center" label="模板组ID" prop="id" width="80px" />
      <el-table-column
        align="center"
        label="模板组名称"
        prop="group_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="模板组标识"
        prop="group_mark"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="操作" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleLook(row)">详情</el-button>
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <msg-detail ref="msgDetail" @fetch-data="fetchData" />
    <msg-edit ref="msgEdit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getMessageTpl, deleteMessageTpl } from '@/api/message'
  import msgDetail from './components/detail'
  import msgEdit from './components/edit'
  export default {
    name: 'Message',
    components: { msgDetail, msgEdit },
    data() {
      return {
        list: [],
        listLoading: true,
        dialogVisible: false,
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前消息模板组吗', null, async () => {
            const { data } = await deleteMessageTpl({ group_id: row.id })
            if (data.is_success) {
              this.$baseMessage(
                '删除成功',
                'success',
                'vab-hey-message-success'
              )
              await this.fetchData()
            }
          })
        } else {
          this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
        }
      },
      handleLook(row) {
        this.$refs['msgDetail'].showEdit(row)
      },
      handleEdit(row) {
        console.log(row)
        if (row.id) {
          this.$refs['msgEdit'].showEdit(row)
        } else {
          this.$refs['msgEdit'].showEdit()
        }
      },
      close() {
        this.detail = ''
        this.dialogVisible = false
        this.fetchData()
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getMessageTpl({})
        this.list = data
        this.listLoading = false
      },
    },
  }
</script>
